import { _decorator, Component, Graphics, Mask, PolygonCollider2D } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('cutObjMask')
export class cutObjMask extends Component {

    protected onLoad(): void {
        this._PolygonCollider2D = this.getComponent(PolygonCollider2D)
        this._mask = this.getComponent(Mask)
        this.scheduleOnce(() => {
            this.refresh()
        }, 0.1)

    }

    start() {

    }

    update(deltaTime: number) {
        // this.refresh()
    }

    _PolygonCollider2D: PolygonCollider2D = null
    _mask: Mask = null
    refresh() {
        const points = this._PolygonCollider2D.points
        const mask = this._mask
        this._PolygonCollider2D.apply()
        // @ts-ignore
        let graphic = mask._graphics
        if (!graphic) {
            graphic = this.node.getComponent(Graphics)
        }

        graphic.clear()
        const len = points.length;
        graphic.moveTo(points[len - 1].x, points[len - 1].y);
        for (let i = 0; i < points.length; i++) {
            graphic.lineTo(points[i].x, points[i].y);
        }
        graphic.fill();
    }
}


